<?php
/*
 * 兼容第三方插件补丁文件
 * @appapi 兼容重整
*/
defined('BY_JYA') or exit('error');
$m = $_SESSION['m'];
if(empty($m)) {
	message('错误访问.');
}
$_RQ['op']=$_RQ['op']?$_RQ['op']:'display';
if($_RQ['op'] == 'display') {
	$params=array('uniacid'=>$_SESSION['uniacid'],'key'=>$_SESSION['m']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
	}
	if(!empty($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
	}
	$fields=array('id');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 15;
	$total='';
	$replies=pdo_getslice('core_back',$params,array($pindex,$psize),$total,$fields);
	if($replies){
		foreach($replies as &$row){
			$row['keyword']=pdo_getcolumn('core_back_keyword',array('back_id'=>$row['id']),'keyword');
		}
	}
	$pager = pagination($total, $pindex, $psize);
	template('platform/reply');
}
if($_RQ['op'] == 'post') {
	if ($_W['isajax'] && $_W['ispost']) {
		exit('success');
	}
	$rid = intval($_RQ['rid']);
	if(!empty($rid)) {
		$reply = pdo_get('core_back',array('uniacid'=>$_SESSION['uniacid'],'id'=>$rid),array('id','status'));
		if($reply){
			$reply['keyword']=pdo_getcolumn('core_back_keyword',array('back_id'=>$reply['id']),'keyword');
		}
		if(empty($reply)) {
			exit('error');
		}
	}
	if($_RQ['submit']) {
		$rule = array(
			//'keyword' =>trim($_RQ['keyword']),
			//'keytype'=>1,
			'status'=>intval($_RQ['status']),
		);
		$module = WeUtility::createModule($m);
		
		if(empty($module)) {
			message('抱歉，模块不存在！');
		}
		$msg = $module->fieldsFormValidate();
		
		if(is_string($msg) && trim($msg) != '') {
			message($msg);
		}
		if (!empty($rid)) {
			$result = pdo_update('core_back', $rule, array('uniacid'=>$_SESSION['uniacid'],'id' => $rid));
			pdo_update('core_back_keyword',array('keyword'=>trim($_RQ['keyword'])),array('back_id'=>$rid));
		} else {
			$rule['uniacid']=$_SESSION['uniacid'];
			$rule['key']=$m;
			$rule['createtime']=TIMESTAMP;
			$result = pdo_insert('core_back', $rule);
			$rid = pdo_insertid();
			$insert=array(
				'back_id'=>$rid,
				'keyword'=>trim($_RQ['keyword']),
				'uniacid'=>$_SESSION['uniacid'],
				'type'=>1
			);
			pdo_insert('core_back_keyword',$insert);
		}
		if (!empty($rid)) {
			$module->fieldsFormSubmit($rid);
			message('回复规则保存成功！', './index.php?action=module&do=rule');
		} else {
			message('回复规则保存失败, 请联系网站管理员！');
		}
	}
	template('platform/reply-post');
}
if($_RQ['op'] == 'delete') {
	$rids = $_RQ['rid'];
	if(!is_array($rids)) {
		$rids = array($rids);
	}
	if(empty($rids)) {
		message('非法访问.');
	}
	foreach($rids as $rid) {
		$rid = intval($rid);
		pdo_delete('core_back',array('uniacid'=>$_SESSION['uniacid'],'id'=>$rid));
		$module = WeUtility::createModule($m);
		if (method_exists($module, 'ruleDeleted')) {
			$module->ruleDeleted($rid);
		}
	}
	message('规则操作成功！', referer());
}
function module_build_form($name, $rid) {
	$rid = intval($rid);
	$m = WeUtility::createModule($name);
	return $m->fieldsFormDisplay($rid);
}